﻿using UnityEngine;
using System.Collections;

public class FlyerConttrollerr : MonoBehaviour
{

    public int Health = 100;

    private Animator anim;
    private bool isDraging = false;
    private Vector3 offset;
    private Gun gun;
    private Flyer flyer;

    private float DoubleMissileTime;


    void Awake()
    {
        flyer = GetComponent<Flyer>();
        anim = GetComponent<Animator>();
        gun = GetComponentInChildren<Gun>();
    }


    void performMissile()
    {
        DoubleMissileTime -= Time.deltaTime;
        if (DoubleMissileTime <= 0)
        {
            gun.MissileIndex = 0;
            DoubleMissileTime = 0;
        }
        else
            gun.MissileIndex = 1;
    }

    void performBomb()
    {
        if(Input.GetMouseButton(1))
        {
            int i = 1;//炸弹
        }
    }

    void performMovement()
    {
        if (isDraging && Input.GetMouseButton(0))
        {
            Vector3 pos = Input.mousePosition - offset;
            flyer.MoveTo(pos);
        }
    }

    void performFire()
    {
        if(mLaucherElapsedTime>mLauchCooldown)
        {
            gun.Launch();
            mLaucherElapsedTime = 0;
        }
        else
        {
            mLaucherElapsedTime += Time.deltaTime;
        }
    }

    void UpdataSingleGame()
    {
        performMissile();
        performBomb();
        if(flyer.IsAlive)
        {
            performMovement();
            performFire();
        }
    }

	// Update is called once per frame
    void Update()
    {
        UpdataSingleGame();
	}

    void GameOver()
    {
        Destroy(gameObject); 
    }

    void MoveTo(Vector3 pos)
    {
        transform.localPosition = pos;
    }

    void OnMouseDown()
    {
        isDraging = true;
        offset = Input.mousePosition - transform.localPosition;
        
    }

    void OnMouseUp()
    {
        isDraging = false;
    }

    float mLaucherElapsedTime = float.MaxValue;
    float mLauchCooldown = 0.3f;


}
